#include "GridLayout.h"

GridLayout::GridLayout(const int& colCount, const int& rowCount) {

	//registro la cantidad de columnas
	this->colCount = colCount;

	//registro la cantidad de filas
	this->rowCount = rowCount;

	//inicializo la matriz de widgets
	this->widgets = new Widget*[colCount * rowCount];

	for(int rowIndex = 0; rowIndex < this->rowCount; rowIndex++)
		for(int colIndex = 0; colIndex < this->colCount; colIndex++)
			this->widgets[ rowIndex * this->colCount + colIndex] = NULL;

	//inicializo el contador de widgets en cero
	this->widgetCount = 0;
}

void GridLayout::add(Widget* widget){

	//si hay lugar para mas widgets, agrego
	if(this->widgetCount < (this->rowCount * this->colCount) )
		this->widgets[this->widgetCount++] = widget;

	//actualizo las dimensiones
	updateSize();
}

void GridLayout::set(Widget* widget, const int& colIndex, const int& rowIndex){

	//inserto el widget en el lugar correspondiente
	this->widgets[rowIndex * this->colCount + colIndex] = widget;

	//actualizo las dimensiones
	updateSize();
}

void GridLayout::draw(SDL_Surface* screen, const int& x, const int& y){
	int widgetX;
	int widgetY;

	//recorro los widgets y los dibujo en la posición correspondiente
	for(int rowIndex = 0; rowIndex < this->rowCount; rowIndex++)
		for(int colIndex = 0; colIndex < this->colCount; colIndex++)
			if(this->widgets[ rowIndex * this->colCount + colIndex] != NULL){

				//calculo las coordenadas para el widget a dibujar
				widgetX = x + colIndex * calcMaxWidth();
				widgetY = y + rowIndex * calcMaxHeight();

				//actualizo las coordenadas del widget
				this->widgets[ rowIndex * this->colCount + colIndex]->setX(widgetX);
				this->widgets[ rowIndex * this->colCount + colIndex]->setY(widgetY);

				//dibujo el widget
				SDLControls::getInstance()->draw(this->widgets[ rowIndex * this->colCount + colIndex], widgetX, widgetY);
				//this->widgets[ rowIndex * this->colCount + colIndex]->draw(screen, widgetX, widgetY);
			}
}

int const GridLayout::calcMaxHeight(){
	int maxHeight = 0;
	int auxHeight;

	//recorro los widgets
	for(int widgetIndex = 0; widgetIndex < this->rowCount * this->colCount; widgetIndex++)
		if(this->widgets[widgetIndex] != NULL){

			//obtengo la altura
			auxHeight = this->widgets[widgetIndex]->getHeight();

			//verifico si es la maxima altura encontrada
			if(auxHeight > maxHeight)
				maxHeight = auxHeight;
		}

	//devuelvo la altura maxima
	return maxHeight;
}

int const GridLayout::calcMaxWidth(){
	int maxWidth = 0;
	int auxWidth;

	//recorro los widgets
	for(int widgetIndex = 0; widgetIndex < this->rowCount * this->colCount; widgetIndex++)
		if(this->widgets[widgetIndex] != NULL){

			//obtengo la altura
			auxWidth = this->widgets[widgetIndex]->getWidth();

			//verifico si es la maxima altura encontrada
			if(auxWidth > maxWidth)
				maxWidth = auxWidth;
		}

	//devuelvo la altura maxima
	return maxWidth;

}

void GridLayout::updateSize(){

	//recalculo las altura total
	setHeight(calcMaxHeight() * this->rowCount);

	//recalculo el ancho total
	setWidth(calcMaxWidth() * this->colCount);
}

void GridLayout::click(const SDL_Event& event){
//	SDLControls::getInstance()->handleMouseClick(this->widgets, this->widgetCount, event);
}

void GridLayout::mouseIn(const SDL_Event& event){
	//SDLControls::getInstance()->handleMouseMotion(this->widgets, this->widgetCount, event);
}

void GridLayout::mouseOut(const SDL_Event& event){
	//SDLControls::getInstance()->handleMouseMotion(this->widgets, this->widgetCount, event);
}

bool GridLayout::isWidgetContainer(){
	return true;
}

GridLayout::~GridLayout() {

	//elimino los widgets
	for(int widgetIndex = 0; widgetIndex < rowCount * colCount; widgetIndex++)
		if(this->widgets[widgetIndex] != NULL)
			delete this->widgets[widgetIndex];

	//elimino los punteros a widgets
	delete[] this->widgets;
}
